<?php
/*
 * @Author: Terry
 * @Date: 2025-03-20 18:25:23
 * @LastEditors: Terry
 * @LastEditTime: 2025-03-23 16:35:02
 * @Description: file content
 * @FilePath: \yyd_api\app\middleware\CheckLogin.php
 */
declare (strict_types = 1);

namespace app\middleware;

use think\facade\Request;
use think\Response;

class CheckLogin
{

    /**
     * 处理请求
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 获取当前路由
        $controller = strtolower($request->controller());
        $action = strtolower($request->action());
        $route = $controller . '/' . $action;
        $uuid = $request->header('uuid');
        $request->uuid = $uuid;
        // 获取token
        $token = $request->header('Authorization');
        if($token=='develop'){ 
            return $next($request);
        }
        if (!$token) {
            return result(UNAUTHORIZED, '登录过期请重新登录');
    }

        // 验证token
        $payload = verifyToken($token);
        if (!$payload) {
            return result(UNAUTHORIZED, '登录已过期，请重新登录');
        }

        // 将用户ID保存到请求对象中，方便后续使用
        $request->userId = $payload->uid;

        return $next($request);
    }
} 